# -*- coding: utf-8 -*-

from xml.dom.minidom import parse

class DebugEntry( object ):
    def __init__( self, level, timestamp, message, thread=None ):
        self.level = level
        self.timestamp = timestamp
        self.message = message
        self.thread = thread
    # __init__()

    def __str__( self ):
        return """\
<debugentry level="%(level)s" timestamp="%(timestamp)s" thread="%(thread)s">
%(message)s
</debugentry>
""" % { "level": self.level,
        "timestamp": self.timestamp,
        "message": self.message,
        "thread": self.thread,
        }
    # __str__()
# DebugEntry


def __get_tag_contents( dom_element ):
    r = []
    for n in dom_element.childNodes:
        if n.nodeType == n.TEXT_NODE:
            r.append( n.data )
    return "".join( r )
# __get_tag_contents()


def read( reader ):
    """Generator that yield L{DebugEntry} of the given reader.

    Reader is any file object, it may be a pipe (os.popen) or a file()
    instance.

    @param reader: any object accepted by xml.dom.minidom.parse
    """
    dom = parse( reader )

    root = dom.documentElement
    for child in root.getElementsByTagName( 'debugentry' ):
        level = child.getAttribute( 'level' )
        timestamp = float( child.getAttribute( 'timestamp' ) )
        thread = child.getAttribute( 'thread' )
        message = __get_tag_contents( child )
        yield DebugEntry( level, timestamp, message, thread )
# read()
